# encoding: utf-8
import os

BASE_DIR = os.path.abspath(os.path.dirname(__file__))


DOCKER_CE_VERSION = "docker-ce-20.10.7"  # docker 版本
KUBELET_VERSION = "kubelet-1.20.9"  # kubelet 版本
KUBEADM_VERSION = "kubeadm-1.20.9"  # kubeadm 版本
KUBECTL_VERSION = "kubectl-1.20.9"  # kubectl 版本

# kubernetes 版本，一般要和上面的组建保持版本一致
KUBERNETES_VERSION = "v1.20.9"
# 镜像仓库
IMAGES_REPOSITORY = "registry.cn-hangzhou.aliyuncs.com/google_containers"
# service 子网地址
SERVICE_CIDR = "10.96.0.0/16"
# pod 子网地址
POD_NETWORK_CIDR = "172.16.0.0/16"


JENKINS_VERSION = ""
# python 版本
# PYTHON_VERSION = "3.9.6"

# helm版本
HELM_VERSION = "v3.7.2"

# 环境初始化shell

INIT_SHELL_LIST = [
    # 将 SELinux 设置为 permissive 模式（相当于将其禁用
    "setenforce 0",
    "sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config",
    # 关闭swap
    "swapoff -a",
    "sed -ri 's/.*swap.*/#&/' /etc/fstab",
    # 配置yum源，安装必要的软件
    "yum install -y git wget chrony yum-utils >/dev/null 2>&1",
    # 创建yum 目录
    "mkdir /etc/yum.repos.d/bak && mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak > /dev/null 2>&1",
    # 下载centos源
    "wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.cloud.tencent.com/repo/centos7_base.repo > /dev/null 2>&1",
    # 下载 epel源
    "wget -O /etc/yum.repos.d/epel.repo http://mirrors.cloud.tencent.com/repo/epel-7.repo >/dev/null 2>&1",
    # 配置docker 源
    "wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo > /dev/null 2>&1 ",
    # 配置k8s 源
    """
cat >/etc/yum.repos.d/kubernetes.repo<<EOF
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
exclude=kubelet kubeadm kubectl
EOF""",
    "yum -y makecache > /dev/null 2>&1",

    # 修改内核参数，为kube-proxy 开启ipvs 加载相应内核模块
    "modprobe br_netfilter",
    "echo 'br_netfilter' > /etc/modules-load.d/br_netfilter.conf",
    """
cat > /etc/sysctl.d/k8s.conf <<EOF 
net.bridge.bridge-nf-call-ip6tables = 1 
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
vm.swappiness=0
EOF
""",
"""
cat > /etc/security/limits.conf <<EOF
* soft nofile 65536
* hard nofile 65536
* soft nproc 65536
* hard nproc 65536
* soft  memlock  unlimited
* hard memlock  unlimited
DefaultLimitNOFILE=102400
DefaultLimitNPROC=102400
EOF
""",

    "sysctl -p /etc/sysctl.d/k8s.conf > /dev/null 2>&1",

    # 同步时间
    "systemctl enable chronyd.service",
    "systemctl start chronyd.service",
    "timedatectl set-timezone Asia/Shanghai",
    "ntpdate ntp1.aliyun.com > /dev/null 2>&1",
    "chronyc sources > /dev/null 2>&1",

    # 安装docker
    "yum remove -y docker docker-ce docker-common docker-selinux docker-engine > /dev/null 2>&1",
    "yum install -y %s > /dev/null 2>&1"%DOCKER_CE_VERSION,
    "systemctl start docker > /dev/null 2>&1",
    "systemctl daemon-reload > /dev/null 2>&1",
    "systemctl enable docker  > /dev/null 2>&1",
    "systemctl restart docker > /dev/null 2>&1",

    # 安装kubelet kubeadm kubectl
    "yum install -y %s %s %s --disableexcludes=kubernetes > /dev/null 2>&1"%(KUBECTL_VERSION,KUBELET_VERSION,KUBEADM_VERSION),
    "systemctl enable kubelet > /dev/null 2>&1",
    "systemctl start kubelet > /dev/null 2>&1",

    # 安装nfs
    "yum install -y nfs-utils",
    "systemctl start nfs",
    "systemctl enable nfs",
    # 创建目录
    "mkdir -p /nfs/data > /dev/null 2>1&",
    ]

